const jwt = require('jsonwebtoken');

const userDB=uniCloud.database().collection("userServeUser")
module.exports = function() {
	// 公用模块用法请参考 https://uniapp.dcloud.io/uniCloud/cf-common
	return {
		userServe:class userServe{
			constructor(){
				this.token=null
			}
			reg(user){
				const returned={
					code:0,
					msg:"注册成功"
				}
				return new Promise((r,j)=>{
					let {username,password}=user
					this.isUserName(username).then(isUserName=>{
						console.log("isUserName: ",isUserName);
						if(!isUserName.code){
							//可以增加一个账号池
							userDB.add(Object.assign(user,{regTime:Date.now()})).then(add=>{
								r(returned)
							}).catch(err=>{
								returned.code=500
								returned.msg="未知错误"
								returned.errMsg=err
								j(returned)
							})
						}else{
							j(isUserName)
						}
					})
				})
			}
			isUserName(name){
				return new Promise((r)=>{
					if(!name){
						r({
							code:400,
							msg:"没有输入"
						})
					}
					const returned={
						code:0,
						msg:"账号未注册,可以继续操作"
					}
					userDB.where({username:name}).get().then(({data})=>{
						if(data.length){
							returned.code=400
							returned.msg="账号已注册，不能使用"
						}
						r(returned)
					}).catch(err=>{
						console.log("isUserName err: ",err);
						r({
							code:500,
							msg:"未知错误",
							errMsg:err
						})
					})
				})
			}
			login({username,password}){
				const returned={
					code:0,
					msg:"登录成功"
				}
				return new Promise((r)=>{
					if(!username){
						returned.code=400;
						returned.msg="账号为空，不可登录"
					}else if(!password){
						returned.code=400;
						returned.msg="密码为空，不可登录"
					}
					if(returned.code===400){
						r(returned)
					}
					userDB.where({username}).get().then(({data})=>{
						if(data.length>0&&data[0].password===password){
							r(returned)
						}else if(data.length>0&&data[0]){
							returned.code=400;
							returned.msg="密码错误"
							r(returned)
						}else{
							returned.code=400;
							returned.msg="账号不存在"
							r(returned)
						}
					})
				})
			}
			// token(user){
			// 	return new Promise((r)=>{
					
					
			// 	})
			// }
			setToken(value){
				if(value){
					this.token=value
				}
			}
			getToken(){
				return this.token
				
			}
		},
		Token:class Token{
			constructor(privateKey="zijid",expTime=86400,options) {
				this.privateKey=privateKey
				this.expTime=expTime
				this.options=options||{
					algorithm:"HS256",
					expiresIn:this.expTime,
					issuer:"ChenBin"
				}
				this.noObject="noObjectdata"+privateKey
			}
			set(content){
				let encrypteContent=content
				encrypteContent={[this.noObject]:encrypteContent}
				let token=jwt.sign(encrypteContent,this.privateKey,this.options)
				return token
			}
			get(ciphertext ){
				let returned={
					code:0,
					msg:"获取token成功",
					data:null
				}
				try{
					let data=jwt.verify(ciphertext ,this.privateKey,this.options)
					returned.data=data[this.noObject]
				}catch(err){
					console.log("err: ",err);
					returned.code=400
					returned.msg="token无效"
				}
				return returned
			}
			has(token){
				let returned={
					code:0,
					msg:"token有效"
				}
				try{
					jwt.verify(ciphertext ,this.privateKey,this.options)
				}catch(err){
					console.log("err: ",err);
					returned.code=400
					returned.msg="token无效"
				}
				return returned
			}
		}
	}
}
